package com.tengfei.api.plan;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.common.utils.poi.ExcelUtil;
import com.tengfei.main.plan.domain.bo.StudyMapBo;
import com.tengfei.main.plan.domain.export.StudyMapExportVo;
import com.tengfei.main.plan.domain.vo.UserChapterProgressVo;
import com.tengfei.main.plan.domain.vo.UserCourseProgressVo;
import com.tengfei.main.plan.domain.vo.UserPlanProgressVo;
import com.tengfei.main.plan.service.IStudyMapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户学习计划进度
 *
 * @author tengfei
 * @date 2022-08-01
 */
@Validated
@Api(value = "学习地图", tags = {"学习计划进度"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/plan/studyMap")
public class StudyMapController extends BaseController {

    private final IStudyMapService iStudyMapService;

    /**
     * 查询当前学习计划下某门课程某个用户的各个章节完成情况
     */
    @ApiOperation("查询当前学习计划下某门课程某个用户的各个章节完成情况")
    @SaCheckPermission("plan:studyMap:list")
    @GetMapping("/chapter/list")
    public R<List<UserChapterProgressVo>> chapterProgressList(@ApiParam("用户id") @NotNull(message = "用户id不能为空") @RequestParam("userId") Long userId,
                                                              @ApiParam("课程id") @NotNull(message = "课程id不能为空") @RequestParam("courseId") Long courseId) {
        return R.ok(iStudyMapService.queryUserChapterProgress(userId, courseId));
    }

    /**
     * 查询当前学习计划下某门课程某个用户的完成情况
     */
    @ApiOperation("查询某个学习计划下某用户课程完成进度情况")
    @SaCheckPermission("plan:studyMap:list")
    @GetMapping("/course/list")
    public R<List<UserCourseProgressVo>> courseProgressList(@ApiParam("用户id") @NotNull(message = "用户id不能为空") @RequestParam("userId") Long userId,
                                                            @ApiParam("学习计划id") @NotNull(message = "学习计划id不能为空") @RequestParam("planId") Long planId) {
        return R.ok(iStudyMapService.queryUserCourseProgress(userId, planId));
    }

    /**
     * 查询当前学习计划下某个用户完成进度情况
     */
    @ApiOperation("查询当前学习计划下某个用户完成进度情况")
    @SaCheckPermission("plan:studyMap:list")
    @GetMapping("/list")
    public R<TableDataInfo<UserPlanProgressVo>> planProgressList(StudyMapBo studyMapBo, PageQuery pageQuery) {
        return R.ok(iStudyMapService.queryUserPlanProgress(studyMapBo, pageQuery));
    }

    /**
     * 导出学习计划列表
     */
    @ApiOperation("导出学习进度列表")
    @SaCheckPermission("plan:studyMap:export")
    @Log(title = "导出学习进度列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StudyMapBo bo, HttpServletResponse response) {
        List<UserPlanProgressVo> list = iStudyMapService.queryList(bo);
        List<StudyMapExportVo> studyMapExportVos = BeanUtil.copyToList(list, StudyMapExportVo.class);
        ExcelUtil.exportExcel(studyMapExportVos, "学习计划", StudyMapExportVo.class, response);
    }


}
